package com.xinjian.jianba.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.MomCommentDto;
import com.xinjian.jianba.dto.MomCommentReplayDto;
import com.xinjian.jianba.dto.MomcommentsVo;
import com.xinjian.jianba.dto.MonCommentsDto;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.MomCommentService;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.utils.JsonUtil;


@RestController
@RequestMapping(value="/api/v3/jianba/MomComment")
public class MomCommentControllor extends AbstractController{

	@Autowired
	private MomCommentService momCommentService;
	@Autowired
	private  ValidatorService validatorService;
	@PostMapping("/comment")
	public String momComment(@RequestBody MomCommentDto momCommentDto){
		try {
			validatorService.valid(momCommentDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		Long comId=momCommentService.saveMomComment(momCommentDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,comId);
	}
	@PostMapping("/replay")
	public String replay(@RequestBody MomCommentReplayDto momCommentReplayDto){
		try {
			validatorService.valid(momCommentReplayDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		Long comId=momCommentService.saveMomCommentReplay(momCommentReplayDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,comId);
	}
	@PostMapping("/mon/comments")
	public String monComments(@RequestBody MonCommentsDto monCommentsDto) throws ParseException{
		try {
			validatorService.valid(monCommentsDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		Map<String,Object> map=momCommentService.findCommentAndReplayByMonId(monCommentsDto);
		map.put("code", ResponseCode.SUCCESS+"");
		map.put("message", ResponseCode.SUCCESS_MSG);
		return  JsonUtil.getJsonString(map);
	}
	@PostMapping("/comment/delete")
	public String commentDelete(@RequestBody Map<String,String> map){
		String commentId=map.get("commentId");
		String userId=map.get("userId");
		if(commentId==null||userId==null||commentId.equals("")||userId.equals("")){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}
		Map<String,Object> delMap=new HashMap<String,Object>();
		delMap.put("userId", Long.parseLong(userId));
		delMap.put("commentId", Long.parseLong(commentId));
		int re=momCommentService.deleteMomcomment(delMap);
		if(re>0){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR, "评论不存在或已删除");
		}
		
	}
	
}
